﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.ComponentModel.DataAnnotations;

namespace XCI.Annotations
{
    /// <summary>
    /// 不为空验证属性
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class NotEmptyAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var error = new ValidationResult(ErrorMessage ?? "不能为空");
            if (value == null) return error;
            if (value is System.Collections.ICollection list && list.Count == 0) return error;
            if (value is Array array && array.Length == 0) return error;
            if (value is string str && str.Length == 0) return error;
            return ValidationResult.Success;
        }
    }
}